package com.ruoyi.system.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ProvideInfo;
import com.ruoyi.system.service.impl.ProvideInfoServiceImpl;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
public class NoModelDataListener7 extends AnalysisEventListener<Map<Integer, String>> {




    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private SysUserServiceImpl sysUserService;
    private SysDeptServiceImpl sysDeptService;

    public NoModelDataListener7(SysUserServiceImpl sysUserService,SysDeptServiceImpl sysDeptService){
        this.sysUserService = sysUserService;
        this.sysDeptService = sysDeptService;
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData(cachedDataList);
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData(cachedDataList);
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库-营运公司
     */
    private void saveData(List<Map<Integer, String>> cachedDataList) {
        log.info("{}条数据，开始存储数据库！", cachedDataList.size());
        try {
            for (Map<Integer,String> map:cachedDataList) {
                log.info(map.toString());
               SysUser sysUser = new SysUser();
                String provide = map.get(3);
                SysDept sysDept = sysDeptService.selectDeptByName(provide);
                if (sysDept != null) {
                    sysUser.setDeptId(sysDept.getDeptId());
                } else {
                    throw new IOException("有不存在的供应商账号-" + provide + "请先添加账号再导入！");
                }
                sysUser.setNickName(map.get(1));
                sysUser.setUserName(map.get(2));
                sysUser.setPhonenumber(map.get(2));
                sysUser.setPassword(SecurityUtils.encryptPassword("Wz@163.com"));
                sysUser.setRoleIds(new Long[]{101L});
                final int i = sysUserService.insertUser(sysUser);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info("存储数据库成功！");
    }
}